package cn.shyouchi.wuguanjia.android.ui.patrol.report;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.entity.MultiItemEntity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.bingoogolapple.photopicker.widget.BGASortableNinePhotoLayout;
import cn.shyouchi.wuguanjia.android.Global;
import cn.shyouchi.wuguanjia.android.R;
import cn.shyouchi.wuguanjia.android.base.BaseActivity;
import cn.shyouchi.wuguanjia.android.event.SelLeaderEvent;
import cn.shyouchi.wuguanjia.android.utils.LogUtil;
import cn.shyouchi.wuguanjia.android.widget.TopBar;

public class PatrolRepairActivity extends BaseActivity {

    @BindView(R.id.topBar)
    TopBar mTopBar;
    @BindView(R.id.tv_report_time)
    TextView mTvReportTime;
    @BindView(R.id.tv_report_state)
    TextView mTvReportState;
    @BindView(R.id.tv_report_addr)
    TextView mTvReportAddr;
    @BindView(R.id.tv_report_title)
    TextView mTvReportTitle;
    @BindView(R.id.tv_report_detail)
    TextView mTvReportDetail;
    @BindView(R.id.rv_leader)
    RecyclerView mRvLeader;
    @BindView(R.id.edit_report_info)
    EditText mEditReportInfo;

    RepairLeaderAdapter mRepairLeaderAdapter;

    List<MultiItemEntity> mLeaderGroups = new ArrayList<>();

    RepairLeaderGroup group;


    PatrolErrorEntity.ResultMessageBean entity;
    @BindView(R.id.photo_snpl)
    BGASortableNinePhotoLayout mPhotoSnpl;

    ArrayList<String> photos = new ArrayList<>();


    @Override
    public int initContentView() {
        return R.layout.activity_patrol_repair;
    }

    @Override
    public void initInjector() {
        entity = getIntent().getParcelableExtra("info");
    }

    @Override
    public void initUiAndListener() {
        ButterKnife.bind(this);
        mTopBar.setTitle(getTitle().toString());
        mTopBar.setLeftIcon(R.mipmap.ic_back);
        setSupportActionBar(mTopBar.getToolbar());
        EventBus.getDefault().register(this);

        group = new RepairLeaderGroup("选择通知人员");
        mLeaderGroups.add(group);
        mRepairLeaderAdapter = new RepairLeaderAdapter(mLeaderGroups);
        mRvLeader.setAdapter(mRepairLeaderAdapter);
        mRvLeader.setLayoutManager(new LinearLayoutManager(mContext));
        mPhotoSnpl.init(this);

        if (entity != null) {
            fillInfo();
        }

    }


    private void fillInfo() {

//        mTvReportOrder.setText(entity.getUniqueid());
//        mTvReportPerson.setText(entity.getUser().getName());

        mTvReportTime.setText(entity.getInspectionpoint().getInspectiontime());
        mTvReportAddr.setText(entity.getInspectionpoint().getInspectionname());
//        mTvReportContent.setText(entity.getInspectionpoint().getInspectioncontent());

        mTvReportTitle.setText(entity.getTitle());
        mTvReportDetail.setText(entity.getContent());

        for (PatrolErrorEntity.ResultMessageBean.ImageBean imageBean : entity.getImage()) {
            photos.add(Global.BASE_URL + imageBean.getImage());
        }
        mPhotoSnpl.setData(photos);

    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isApplyStatusBarColor() {
        return true;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuItem menuItem = menu.add("提交");
        menuItem.setShowAsAction(2);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getTitle().equals("提交")) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onLeaderSel(SelLeaderEvent event) {
        if (group.getSubItems() != null) {
            group.getSubItems().clear();
        }
        for (RepairLeaderSubitem subitem : event.getLeaderSubitems()) {
            group.addSubItem(subitem);
        }

        LogUtil.e("size = " + event.getLeaderSubitems().size());
        mRepairLeaderAdapter.notifyDataSetChanged();
        for (int i = 0; i < mLeaderGroups.size(); i++) {
            mRepairLeaderAdapter.expandAll(i, false, true);
        }


    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
